package servletDemo;

import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringReader;
import java.nio.charset.StandardCharsets;
import java.util.Map;

/**
 * @ClassName loServlet
 * @Description HTTP相关
 */
@WebServlet(urlPatterns = {"/lo1", "/lo2"})
public class loServlet extends HttpServlet {


    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setHeader("content-type", "text/html; charset=utf-8");
        /*request 获取请求行数据*/
//        获取请求方式
        String method = req.getMethod();
//        获取协议版本号
        String protocol = req.getProtocol();
//        获取请求方的ip地址
        String address = req.getRemoteAddr();
//        获取访问的servlet的虚拟路径
        String servlet_path = req.getServletPath();
//        获取虚拟目录（项目访问路径）
        String context_path = req.getContextPath();
//        获取URL（统一资源定位符）
        StringBuffer requestUrl = req.getRequestURL();
//        获取URI（统一资源标识符）
        String requestUri = req.getRequestURI();
//        获取请求参数
        String queryString = req.getQueryString();

        /*request 获取请求头数据*/
//        根据请求头中的名称获取相应的值
        String host = req.getHeader("Host");


        /*获取请求参数数据*/
//        获取所有参数Map集合
        Map<String, String[]> reqArgs = req.getParameterMap();
//        根据名称获取数参数值  数组
        String[] values = req.getParameterValues("args name");
//        根据名称获取数参数值  单个值
        String value = req.getParameter("args name");

        String username = req.getParameter("username");
//        *** 解决乱码问题 ***
        System.out.println("乱码的用户名："+username);
        // 编码
        byte[] bytes = username.getBytes(StandardCharsets.ISO_8859_1);
        // 解码
        username = new String(bytes,StandardCharsets.UTF_8);
        System.out.println("正确的用户名: "+username);
        resp.getWriter().write("<h5>欢迎您，"+username+"! </h5>");

        /* response 设置响应数据 */
//        设置响应状态码
        resp.setStatus(200);
//        设置请求头的键值对
        resp.setHeader("key", "value");
//        获取字符输出流
        PrintWriter printWriter = resp.getWriter();
//        获取字节输出流
        ServletOutputStream servletOutputStream = resp.getOutputStream();


    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("do post exec");
        /*request 获取请求体数据*/
//        获取字节流数据
//        ServletInputStream servletInputStream = req.getInputStream();
//        获取字符流数据
//        BufferedReader bufferedReader = req.getReader();
        // 读取一行数据
//        String line = bufferedReader.readLine();

//        post解决乱码
        resp.setHeader("content-type", "text/html; charset=utf-8");
        req.setCharacterEncoding("UTF-8");
        String username = req.getParameter("username");
        System.out.println("用户名: "+username);
        resp.getWriter().write("<h5>欢迎您，"+username+"! </h5>");
    }
}
